package mya.mes.mdc.model.tag.values;


import mya.mes.mdc.model.alarm.AlarmHolder;
import mya.mes.mdc.model.tag.MdcTagValue;
import mya.mes.mdc.model.tag.ViewTag;
import mya.mes.mdc.model.tag.convert.PlcTagType;

import java.util.ArrayList;
import java.util.List;

public class ViewTagValue {

    public String viewTagName;

    public ViewTag viewTag;

    public List<MdcTagValue> tagValueList = new ArrayList<>();

    public List<MdcTagValue> alarmTagValueList = new ArrayList<>();


    public boolean HAS_DIGITS = false;

    public MdcTagValue[] DIGITS_ARRAY = new MdcTagValue[4];


    public boolean HAS_POSTE = false;
    public MdcTagValue[] POSTE_ARRAY = new MdcTagValue[3];

    public AlarmHolder alarmHolder;

    public boolean hasAlarm() {
        return alarmHolder != null && !alarmHolder.isAllEmpty();
    }


    public boolean hasStartAlarm() {
        return alarmHolder != null && !alarmHolder.isListStartEmpty();
    }

    public boolean hasStopAlarm() {
        return alarmHolder != null && !alarmHolder.isListStopEmpty();
    }

    public boolean isDigitsValid() {
        if (!HAS_DIGITS) {
            return true;
        }

        for (int i = 0; i < DIGITS_ARRAY.length; i++) {
            if (DIGITS_ARRAY[i] == null) {
                return false;
            }
        }

        return true;
    }

    public boolean isPosteValid() {
        if (!HAS_POSTE) {
            return true;
        }

        for (int i = 0; i < POSTE_ARRAY.length; i++) {
            if (POSTE_ARRAY[i] == null) {
                return false;
            }
        }

        return true;
    }

    public void add(MdcTagValue tagValue) {

        String plcTagType = tagValue.mdcTag.plcTagType;
        if (PlcTagType.DEF.equals(plcTagType) || PlcTagType.SYSTEM.equals(plcTagType)) {
            alarmTagValueList.add(tagValue);
            return;
        }
        if (plcTagType.startsWith("_DIGITS_")) {
            //  String _DIGITS_21 = "_DIGITS_21";
            //  String _DIGITS_43 = "_DIGITS_43";
            //  String _DIGITS_65 = "_DIGITS_65";
            //  String _DIGITS_87 = "_DIGITS_87";
            HAS_DIGITS = true;
            if (PlcTagType._DIGITS_21.equals(plcTagType)) {
                DIGITS_ARRAY[0] = tagValue;
                return;
            }
            if (PlcTagType._DIGITS_43.equals(plcTagType)) {
                DIGITS_ARRAY[1] = tagValue;
                return;
            }
            if (PlcTagType._DIGITS_65.equals(plcTagType)) {
                DIGITS_ARRAY[2] = tagValue;
                return;
            }
            if (PlcTagType._DIGITS_87.equals(plcTagType)) {
                DIGITS_ARRAY[3] = tagValue;
                return;
            }
        }

        if (plcTagType.startsWith("_POSTE")) {
            //  String _POSTE1 = "_POSTE1";
            //  String _POSTE2 = "_POSTE2";
            //  String _POSTE3 = "_POSTE3";
            HAS_POSTE = true;
            if (PlcTagType._POSTE1.equals(plcTagType)) {
                POSTE_ARRAY[0] = tagValue;
                return;
            }
            if (PlcTagType._POSTE2.equals(plcTagType)) {
                POSTE_ARRAY[1] = tagValue;
                return;
            }
            if (PlcTagType._POSTE3.equals(plcTagType)) {
                POSTE_ARRAY[2] = tagValue;
                return;
            }
        }
        tagValueList.add(tagValue);
    }


}
